//
//  StarView.m
//  Mdongying
//
//  Created by mac pro -chenweishuai on 14-3-10.
//  Copyright (c) 2014年 伟帅陈. All rights reserved.
//

#import "StarView.h"
#import "AppUtility.h"
#import "PrefixHeader.pch"
#define ForegroundStarImage @"Mypingjia03"
#define BackgroundStarImage @"Mypingjia02"

#define ForegroundStarImage1 @"WhiteFullStar"
#define BackgroundStarImage1 @"WhiteStar"
typedef void(^completeBlock)(CGFloat currentScore);

@interface StarView()

@property (nonatomic, strong) UIView *foregroundStarView;
@property (nonatomic, strong) UIView *backgroundStarView;

@property (nonatomic, assign) NSInteger numberOfStars;

@property (nonatomic,strong)completeBlock complete;

@end

@implementation StarView
//
//@synthesize StarDelegate = _StarDelegate;
//- (id)initWithFrame:(CGRect)frame WithHaveXingNum:(int)xingNum CanTouch:(BOOL)isTouch
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        CGRect frame = self.frame;
//        int Height = self.frame.size.height;
//        frame.size.width = Height+(Height+Height/4)*4;
//        self.frame = frame;
//        self.backgroundColor = [UIColor clearColor];
//        for (int i=0;i<5; i++) {
//
//            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//            [btn setFrame:CGRectMake((Height+Height/4)*i,0,Height, Height)];
//            btn.backgroundColor = [UIColor clearColor];
//            btn.tag = 200+i;
//            btn.userInteractionEnabled = isTouch;
//            [btn addTarget:self action:@selector(ChanegStarNum:) forControlEvents:UIControlEventTouchUpInside];
//            if (i<xingNum) {
//
//                [btn setImage:[AppUtility getLocalImage:@"Mypingjia03"] forState:UIControlStateNormal];
//            }
//            else
//            {
//                [btn setImage:[AppUtility getLocalImage:@"Mypingjia02"] forState:UIControlStateNormal];
//            }
//
//            [self addSubview:btn];
//
//        }
//
//
//    }
//    return self;
//}
//
//-(void)ChanegStarNum:(UIButton *)btn
//{
//    for (int i = 0; i<5; i++) {
//
//        UIButton *bb = (UIButton *)[self viewWithTag:200+i];
//        if (i<=btn.tag-200) {
//
//            [bb setImage:[AppUtility getLocalImage:@"Mypingjia03"] forState:UIControlStateNormal];
//        }
//        else
//        {
//            [bb setImage:[AppUtility getLocalImage:@"Mypingjia02"] forState:UIControlStateNormal];
//        }
//    }
//
//    [self.StarDelegate ChooseStarViewDelegate:btn.tag-200+1];
//
//}
//
//-(void)ToChanegStarNum:(int)num
//{
//    for (int i = 0; i<5; i++) {
//
//        UIButton *bb = (UIButton *)[self viewWithTag:200+i];
//        if (i<=num-1) {
//
//            [bb setImage:[AppUtility getLocalImage:@"Mypingjia03"] forState:UIControlStateNormal];
//        }
//        else
//        {
//            [bb setImage:[AppUtility getLocalImage:@"Mypingjia02"] forState:UIControlStateNormal];
//        }
//    }
//
//}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _numberOfStars = 5;
        _rateStyle = WholeStar;
        [self createStarView];
        
    }
    return self;
    
}
-(instancetype)initWithFrame:(CGRect)frame numberOfStars:(NSInteger)numberOfStars rateStyle:(RateStyle)rateStyle isAnination:(BOOL)isAnimation delegate:(id)delegate{
    if (self = [super initWithFrame:frame]) {
        _numberOfStars = numberOfStars;
        _rateStyle = rateStyle;
        _isAnimation = isAnimation;
        _delegate = delegate;
        [self createStarView];
    }
    return self;
    
}
-(instancetype)initWithFrame1:(CGRect)frame numberOfStars:(NSInteger)numberOfStars rateStyle:(RateStyle)rateStyle isAnination:(BOOL)isAnimation delegate:(id)delegate{
    if (self = [super initWithFrame:frame]) {
        _numberOfStars = numberOfStars;
        _rateStyle = rateStyle;
        _isAnimation = isAnimation;
        _delegate = delegate;
        [self createStarView1];
    }
    return self;
    
}
#pragma mark - block方式
-(instancetype)initWithFrame:(CGRect)frame finish:(finishBlock)finish{
    if (self = [super initWithFrame:frame]) {
        _numberOfStars = 5;
        _rateStyle = WholeStar;
        _complete = ^(CGFloat currentScore){ finish(currentScore);
            
        };
        [self createStarView];
        
    }
    return self;
    
}
-(instancetype)initWithFrame:(CGRect)frame numberOfStars:(NSInteger)numberOfStars rateStyle:(RateStyle)rateStyle isAnination:(BOOL)isAnimation finish:(finishBlock)finish{
    if (self = [super initWithFrame:frame]) {
        _numberOfStars = numberOfStars;
        _rateStyle = rateStyle;
        _isAnimation = isAnimation;
        _complete = ^(CGFloat currentScore){
            finish(currentScore);
            
        };
        [self createStarView];
        
    }
    return self;
    
}
#pragma mark - private Method
-(void)createStarView{
    
    self.foregroundStarView = [self createStarViewWithImage:ForegroundStarImage];
    self.backgroundStarView = [self createStarViewWithImage:BackgroundStarImage];

    self.foregroundStarView.frame = CGRectMake(0, 0, self.bounds.size.width*_currentScore/self.numberOfStars, self.bounds.size.height);
    [self addSubview:self.backgroundStarView];
    [self addSubview:self.foregroundStarView];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTapRateView:)];
    tapGesture.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tapGesture];
    
}
-(void)createStarView1{
    
    self.foregroundStarView = [self createStarViewWithImage:ForegroundStarImage1];
    self.backgroundStarView = [self createStarViewWithImage:BackgroundStarImage1];
    
    
    self.foregroundStarView.frame = CGRectMake(0, 0, self.bounds.size.width*_currentScore/self.numberOfStars, self.bounds.size.height);
    [self addSubview:self.backgroundStarView];
    [self addSubview:self.foregroundStarView];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTapRateView:)];
    tapGesture.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tapGesture];
    
}
-(UIView *)createStarViewWithImage:(NSString *)imageName {
    UIView *view = [[UIView alloc] initWithFrame:self.bounds];
    view.clipsToBounds = YES;
    view.backgroundColor = [UIColor clearColor];
    for (NSInteger i = 0; i < self.numberOfStars; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(i * self.bounds.size.width / self.numberOfStars, 0, self.bounds.size.width / self.numberOfStars, self.bounds.size.height);
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [view addSubview:imageView];
        
    }
    return view;
    
}
-(void)userTapRateView:(UITapGestureRecognizer *)gesture {
    CGPoint tapPoint = [gesture locationInView:self];
    CGFloat offset = tapPoint.x; CGFloat realStarScore = offset / (self.bounds.size.width / self.numberOfStars);
    switch (_rateStyle) {
        case WholeStar: {
        self.currentScore = ceilf(realStarScore);
            break;
            
        }
        case HalfStar: self.currentScore = roundf(realStarScore)>realStarScore ? ceilf(realStarScore):(ceilf(realStarScore)-0.5);
        break;
        case IncompleteStar: self.currentScore = realStarScore;
            break;
        default: break;
            
    }
    
}
-(void)layoutSubviews {
    [super layoutSubviews];
    __weak StarView *weakSelf = self;
    CGFloat animationTimeInterval = self.isAnimation ? 0.2 : 0;
    [UIView animateWithDuration:animationTimeInterval animations:^{ weakSelf.foregroundStarView.frame = CGRectMake(0, 0, weakSelf.bounds.size.width * weakSelf.currentScore/self.numberOfStars, weakSelf.bounds.size.height);
        
    }];
    
}
-(void)setCurrentScore:(CGFloat)currentScore {
    if (_currentScore == currentScore) {
        return;
        
    }
    if (currentScore < 0) {
        _currentScore = 0;
        
    } else if (currentScore > _numberOfStars) {
        _currentScore = _numberOfStars; }
    else { _currentScore = currentScore;
            
    }
    if ([self.delegate respondsToSelector:@selector(starRateView:currentScore:)]) {
        [self.delegate starRateView:self currentScore:_currentScore];
        
    }
    if (self.complete) {
        _complete(_currentScore);
        
    }
    [self setNeedsLayout];
    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
